package com.bdy.lm.taximanager.port;

import java.io.File;
import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import android_serialport_api.SerialPort;

public class TSerialPort {
	
	private static final String TAG = "TSerialPort";
	private TSerialPortDelegate mTSerialPortDelegate = null;
	private SerialPort mSerialPort = null;
	private ReadThread mReadThread;
	
	public TSerialPort(TSerialPortDelegate delegate) {
		mTSerialPortDelegate = delegate;
	}
	
	public boolean open(String path, int baudrate) throws SecurityException, IOException, InvalidParameterException {
		if (mSerialPort == null) {
			if (path.length() == 0) {
				throw new InvalidParameterException();
			}
			mSerialPort = new SerialPort(new File("/dev/"+path), baudrate, 0);
		}
		if (mSerialPort == null) {
			return false;
		}

		if (mReadThread == null) {
			mReadThread = new ReadThread();
			mReadThread.start();
		}
		if (mReadThread == null) {
			return false;
		}

		return true;
	}
	
	public void close() {
		if (mSerialPort != null) {
			mSerialPort.close();
			mSerialPort = null;
		}
		
		if (mReadThread != null) {
			mReadThread.interrupt();
			mReadThread = null;
		}
	}
	
	public boolean sendData(byte[] buffer) {
		if (mSerialPort == null) {
			return false;
		}
		try {
			mSerialPort.getOutputStream().write(buffer);
			Log.d("SerialPort", "串口数据已发送");
		}
		catch (IOException e) {
			return false;
		}
		return true;
	}

	private List data = new ArrayList();
	private class ReadThread extends Thread {
		@Override
		public void run() {
		    super.run();
			data.clear();
		    while (!isInterrupted()) {
		    	try {
		    		byte[] buffer = new byte[512];
		    		if (mSerialPort.getInputStream() == null) {
		    			return;
		    		}
					int length = mSerialPort.getInputStream().read(buffer);
		    		if (length > 0) {
                        System.out.print("SerialPort: length = " + length);
						for (int i = 0; i < length; i++) {
							data.add(buffer[i]);
							System.out.print(Integer.toHexString(buffer[i] & 0xff) + " ");
							//Log.d("SerialPortNative", "" + Integer.toHexString(buffer[i] & 0xff));
						}
		    		}

		    		if((data.size() == 18) && ((byte)data.get(6) == 0) && ((byte)data.get(7) == -25)) {//上车
						Log.d("SerialPort", "上车 长度18");
						print();
						mTSerialPortDelegate.processSerialPortData(data);
						data.clear();
						//Log.d("SerialPort", "已清除" + data.size());
					} else if((data.size() == 79) && ((byte)data.get(6) == 0) && ((byte)data.get(7) == -24)) {//下车
						Log.d("SerialPort", "下车 长度79");
						print();
						mTSerialPortDelegate.processSerialPortData(data);
						data.clear();
						//Log.d("SerialPort", "已清除" + data.size());
					} else if((data.size() == 27) && ((byte)data.get(6) == -15) && ((byte)data.get(7) == 1)) {//查询里程点
						Log.d("SerialPort", "查询里程点 长度27");
						print();
						mTSerialPortDelegate.processSerialPortData(data);
						data.clear();
					} else if((data.size() == 24) && ((byte)data.get(6) == -15) && ((byte)data.get(7) == -24)) {//重车过程
						Log.d("SerialPort", "重车过程 长度24");
						print();
						mTSerialPortDelegate.processSerialPortData(data);
						data.clear();
					} else if((data.size() == 11) && ((byte)data.get(6) == 0) && ((byte)data.get(7) == 5)) {//修改运价返回
						Log.d("SerialPort", "修改运价返回 长度11");
						print();
						mTSerialPortDelegate.processSerialPortData(data);
						data.clear();
					} else if((data.size() == 75) && ((byte)data.get(6) == 0) && ((byte)data.get(7) == 4)) {//查询运价返回
						Log.d("SerialPort", "查询运价返回 长度75");
						print();
						mTSerialPortDelegate.processSerialPortData(data);
						data.clear();
					} else if((data.size() == 19) && ((byte)data.get(6) == -15) && ((byte)data.get(7) == 14)) {//查询时间和K值响应
						Log.d("SerialPort", "查询时间和K值响应 长度19");
						print();
						mTSerialPortDelegate.processSerialPortData(data);
						data.clear();
					} else if((data.size() == 11) && ((byte)data.get(6) == -15) && ((byte)data.get(7) == 15)) {//修改时间和K值响应
                        Log.d("SerialPort", "修改时间和K值响应 长度11");
                        print();
                        mTSerialPortDelegate.processSerialPortData(data);
                        data.clear();
                    }
		    	} catch (IOException e) {
		    		e.printStackTrace();
		    		return;
		    	}
		    }
		}
	}
	void print() {
		System.out.println();
		System.out.println("SerialPortList size = " + data.size() + " 数据：" + data.toString());
		//for(int i = 0; i < data.size(); i++) {
		//	Log.d("SerialPort", i + ":" + Integer.toHexString(((byte)data.get(i)) & 0xff));
		//}

	}

	public interface TSerialPortDelegate {
		void processSerialPortData(List data);
	}
}
